var PATH_PREFIX = "";
function setPathPrefix(path) {
	PATH_PREFIX = path;
}

$(function() {
	$('#sendType').select2({
		placeholder: '发送类型'
	});
    //注册清空事件
    WASP_WIDGET.registerResetClearEvent();
})

$(function(){
	var $msgConfList = $('#msgConfList');
	$msgConfList.jqGrid({
		caption: '主消息配置列表<button type="button" class="btn btn-default btn-sm btn-small btn-fullscreen" title="" tabindex="-1"><i class="fa fa-arrows-alt icon-fullscreen"></i></button>',
		url:  PATH_PREFIX+'msgconf/msgConfList.do',
		datatype: "json",
		colNames: ['主消息编号','标题','消息代码','发送类型','runStatus','运行状态','操作'],  
		colModel: [
				  {name:'id',index:'id',hidden:true, align:'left',key:true},
				  {name:'title',index:'title',align:'left',sortable:false},
				  {name:'msgCode',index:'msgCode',align:'left',sortable:false},
				  {name:'sendTypeNm',index:'sendTypeNm',align:'left',sortable:false},
				  {name:'runStatus',index:'runStatus',align:'left',sortable:false,hidden:true},
				  {name:'runStatusNm',index:'runStatusNm',align:'left',sortable:false},
				  {name:'option',index:'option',  resizable:true, align:'left', sortable:false}
				  ],
		rowNum:20,        
		rowList:[20,30,50],
		rownumbers : true,
		rownumWidth : 50,
		prmNames: {
			search: "search",
			page: "pageNo",     //当前页
			rows: "limit"         //每页行数
		},  
		height: 'auto',
		autowidth : true,
		width: false,
		editurl : '',
		viewrecords : true,
		cellEdit : false,
		shrinkToFit : true,
		grouping : false,
		autowidth : true,
		jsonReader: {  
			root: "items",       //结果集
			records: "total", //总记录数 
			total: "pageCount",	  //总页数
			page: "pageNo",	  //当前页 
			repeatitems : false       // (4)  
		},
		pager: "#msgConfPage", 
		viewrecords: true,
		hidegrid : false,
		multiselect:false,
		ondblClickRow : function(msgConfCode) {
			expandProject("msgConfList", msgConfCode);
		},
		
		gridComplete: function(){
			
			var ids = $msgConfList.jqGrid('getDataIDs');
			for(var i=0;i < ids.length;i++){
				var id = ids[i];
				var rowData = jQuery("#msgConfList").jqGrid('getRowData', id);
				
				var st=rowData.runStatus;
				var ve = '<a href="javascript:void(0);" class="btn btn-link btn-jqgrid" title="详情" onclick="viewMsgConf(\''+id+'\');"  >详情</a>';
				var update = '<a date-id="' + id + '" href="javascript:void(0);" class="btn btn-link btn-jqgrid" title="修改" data-toggle="modal" onclick="updateMsgConf(\''+id+'\');" data-target="#modal-edit">修改</a>';
				var status;
				if(st=='S'){
					status='<a href="javascript:void(0);" class="btn btn-link btn-jqgrid" title="运行" onclick="runMsgConf(\''+id+'\');"  >运行</a>';	
				}else{
					status= '<a href="javascript:void(0);" class="btn btn-link btn-jqgrid" title="停止" onclick="stopMsgConf(\''+id+'\');"  >停止</a>';
				}
				
				jQuery("#msgConfList").jqGrid('setRowData',ids[i],{option:ve+update+status});
			}
			
		}
		
	});

	
	$msgConfList.navGrid('#msgConfPage', {
		edit : false,
		add : false,
		del : false,
		search : false,
		refreshstate : 'current'
	});
	$msgConfList.jqGrid('setFrozenColumns');
	jqGridResize($msgConfList);	
});

function updateMsgConf(id){
	var actionUrl =  PATH_PREFIX+"msgconf/msgConfUpdateView.do?msgConfId=" + id;
	openDialog(actionUrl);
}
function viewMsgConf(id){
	var actionUrl =  PATH_PREFIX+"msgconf/msgConfDetailView.do?msgConfId=" + id;
	openDialog(actionUrl);
}

function runMsgConf(id){
	ctools.confirm("您确认启动执行此消息配置！",function(){
		$("#hiddenmsgconfid").val(id);
		var actionUrl = PATH_PREFIX+"msgconf/gotoStart.do?msgConfId=" + id;
		openDialog(actionUrl);
	},"消息配置启动提示");
}

function stopMsgConf(id){
	ctools.confirm("您确认停止执行此消息配置！",function(){
		$("#hiddenmsgconfid").val(id);
		var actionUrl = PATH_PREFIX+"msgconf/gotoStop.do?msgConfId=" + id;
		openDialog(actionUrl);
	},"消息配置停止提示");
}

function queryByCondtion (flag) {
	var title = $.trim($("#title").val());
	var msgCode = $.trim($("#msgCode").val());
	var sendType = $.trim($("#sendType").val());
    var postData = $("#msgConfList").jqGrid("getGridParam", "postData");
    //将filters参数串加入postData选项  
    $.extend(postData,{
    	'sp[title]':title,
    	'sp[msgCode]':msgCode,
    	'sp[sendType]':sendType
    });
    if (flag) {
    	$("#msgConfList").trigger("reloadGrid",[{page:1}]);//重新载入Grid表格
    } else {
    	$("#msgConfList").trigger("reloadGrid");//重新载入Grid表格
	}
}

function refreshMsgEmpConfRs(){
		 var actionUrl = PATH_PREFIX+"msgconf/refreshMsgEmpConfRs.do";
		 openDialog(actionUrl);
}

function addMsgConf(){
	 var actionUrl = PATH_PREFIX+"msgconf/msgConfAddView.do";
	 openDialog(actionUrl);
}

//绑定回车事件,查询
$("body").keypress(function(event){
	var key = event.which;
	if(key == "13"){
		queryByCondtion(true);
	}
});